﻿// ElementTreeView.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "ElementTreeView.h"
#include "ElementSplitterWnd.h"

// CElementTreeView

#define		ELEM_TREE_ROOT_ICON	0
#define		ELEM_TREE_ELEM_ICON	1
#define		ELEM_TREE_SUBE_ICON	2
#define		ELEM_TREE_ITEM_ICON	3

IMPLEMENT_DYNCREATE(CElementTreeView, CTreeView)

CElementTreeView::CElementTreeView()
{
	m_bSubclassed = FALSE;
}

CElementTreeView::~CElementTreeView()
{
}

BEGIN_MESSAGE_MAP(CElementTreeView, CTreeView)
	ON_WM_CREATE()
	ON_NOTIFY_REFLECT(NM_CLICK, &CElementTreeView::OnNMClick)
	ON_NOTIFY_REFLECT(NM_DBLCLK, &CElementTreeView::OnNMDblclk)
	ON_NOTIFY_REFLECT(NM_RCLICK, &CElementTreeView::OnNMRClick)
	ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, &CElementTreeView::OnTvnItemExpanding)
	ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, &CElementTreeView::OnTvnItemExpanded)
	
END_MESSAGE_MAP()


// CElementTreeView 诊断

#ifdef _DEBUG
void CElementTreeView::AssertValid() const
{
	CTreeView::AssertValid();
}

#ifndef _WIN32_WCE
void CElementTreeView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}
#endif
#endif //_DEBUG


// CElementTreeView 消息处理程序


int CElementTreeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CTreeView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	//CTreeCtrl& tree = GetTreeCtrl();
	CElementTreeCtrl& tree = (CElementTreeCtrl&)GetTreeCtrl();
	tree.AttachBitmap();
	
	//CBitmap bmp;
	//if (bmp.LoadBitmap(IDB_BITMAP_ELEMENT_TREE))
	//{
	//	m_il.Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 0);
	//	m_il.Add(&bmp, RGB(255, 0, 255));
	//	int nCount = m_il.GetImageCount();
	//	for (int i = 1; i < nCount; i++)         //support 16
	//	{
	//		m_il.SetOverlayImage(i, i);
	//	}
	//	tree.SetImageList(&m_il, TVSIL_NORMAL);
	//}
	
	
	tree.Populate();

	//InitTreeCtrl();

	return 0;
}


BOOL CElementTreeView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此添加专用代码和/或调用基类
	cs.style |= TVS_HASLINES | TVS_SHOWSELALWAYS | TVS_HASBUTTONS | TVS_NOHSCROLL | TVS_EX_FADEINOUTEXPANDOS;

	return CTreeView::PreCreateWindow(cs);
}

void CElementTreeView::OnInitialUpdate()
{
	CTreeView::OnInitialUpdate();

	// TODO: 在此添加专用代码和/或调用基类
	//
	// 进行子类化的代码
	//
	CTreeCtrl& tree = GetTreeCtrl(); // 得到CTreeCtrl对象的引用
	HWND hwndTreeCtrl = tree.m_hWnd; // 记录下它的句柄，下面马上会用到
	tree.UnsubclassWindow(); // 因为这个窗口已经是子类化过的（用Spy++可以看出来），要先进行UnsubclassWindow
	m_wndTreeCtrl.SubclassWindow(hwndTreeCtrl); // 现在实现我的子类化
	m_bSubclassed = TRUE; // 设置标志

	// 下面这句很重要，否则很多API调用会认为CMyTreeView内部的控件窗口句柄无效
	m_hWnd = m_wndTreeCtrl.m_hWnd; // 修正CMyTreeView的m_hWnd成员变量
}


CTreeCtrl& CElementTreeView::GetTreeCtrl() const
{
	if (m_bSubclassed)
	{
		return (CTreeCtrl&)m_wndTreeCtrl; // 若子类化了，返回新的引用
	}
	else
	{
		return CTreeView::GetTreeCtrl(); // 若未子类化，调用默认处理
	}
}

/************************************************************************/
/* 左键单击事件                                                          */
/************************************************************************/
void CElementTreeView::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult)
{
	CTreeCtrl& tree = GetTreeCtrl();

	CPoint pt;
	if (!GetCursorPos(&pt))
	{
		return;
	}

	tree.ScreenToClient(&pt);
	UINT uFlag = 0;
	HTREEITEM hItem = tree.HitTest(pt, &uFlag);
	if ((NULL == hItem) || !(TVHT_ONITEM&uFlag))
	{
		return;
	}
	
	tree.Select(hItem, TVGN_CARET);
	
	//更新ListView
	CElementSplitterWnd* pWnd = (CElementSplitterWnd*)(this->GetParent());
	unsigned int nElemCode = tree.GetItemData(hItem);
	//pWnd->PopulateList(1, tree.GetItemText(hItem));
	pWnd->PopulateList(nElemCode, tree.GetItemText(hItem));
}

/************************************************************************/
/* 左键双击事件                                                          */
/************************************************************************/
void CElementTreeView::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult)
{
	CTreeCtrl& tree = GetTreeCtrl();

	CPoint pt;
	if (!GetCursorPos(&pt))
	{
		return;
	}

	tree.ScreenToClient(&pt);
	UINT uFlag = 0;
	HTREEITEM hItem = tree.HitTest(pt, &uFlag);
	if ((NULL == hItem) || !(TVHT_ONITEM&uFlag))
	{
		return;
	}

	//显示下一级
	tree.Select(hItem, TVGN_CARET);
	z_uint32 nCode = (z_uint32)tree.GetItemData(hItem);
	//PopulateChildren(nCode, hItem);

	*pResult = 0;
}

/************************************************************************/
/* 右键单击事件                                                          */
/************************************************************************/
void CElementTreeView::OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult)
{
	CTreeCtrl& tree = GetTreeCtrl();

	CPoint pt;
	if (!GetCursorPos(&pt))
	{
		return;
	}

	tree.ScreenToClient(&pt);
	UINT uFlag = 0;
	HTREEITEM hItem = tree.HitTest(pt, &uFlag);
	if ((NULL == hItem) || !(TVHT_ONITEM&uFlag))
	{
		return;
	}


	tree.Select(hItem, TVGN_CARET);
	CString strText = tree.GetItemText(hItem);
	AfxMessageBox(strText);

	*pResult = 0;
}

void CElementTreeView::OnTvnItemExpanding(NMHDR *pNMHDR, LRESULT *pResult) 
{
	//AfxMessageBox("Expanding");
	*pResult = 0;
}

void CElementTreeView::OnTvnItemExpanded(NMHDR *pNMHDR, LRESULT *pResult)
{
	//AfxMessageBox("Expanded");
	*pResult = 0;
}

void CElementTreeView::Populate()
{
	CElementTreeCtrl& tree = (CElementTreeCtrl&)GetTreeCtrl();
	tree.Populate();
}


//void CElementTreeView::Populate()
//{
//	CTreeCtrl& tree = GetTreeCtrl();
//
//	tree.DeleteAllItems();
//	tree.InsertItem("质量元素库", ELEM_TREE_ROOT_ICON, ELEM_TREE_ROOT_ICON, TVI_ROOT);
//	HTREEITEM hRoot = tree.GetRootItem();
//
//	//更新ListView
//	CElementSplitterWnd* pListWnd = (CElementSplitterWnd*)(this->GetParent());
//	pListWnd->CleanupList();
//
//	ck::ElementRepository* pRepository = ck::ElementRepository::GetInstance();
//	ck::CheckElement* pElement = NULL;
//	ck::EnumCheckElement* pElements = NULL;
//	pElements = pRepository->GetElements();
//	if (pElements)
//	{
//		pElements->Reset();
//		while ((pElement = pElements->Next()))
//		{
//			const char* szName = pElement->GetName();
//			z_uint32 code = pElement->GetCode();
//			HTREEITEM hItem = tree.InsertItem(szName, ELEM_TREE_ELEM_ICON, ELEM_TREE_ELEM_ICON, hRoot);
//			tree.SetItemData(hItem, (DWORD)code);
//
//			pListWnd->AddCheckElement(pElement);
//		}
//		tree.Expand(hRoot, TVE_EXPAND);
//
//
//
//		pElements->Release();
//	}
//}


